/*     */ package com.zimbra.cs.dav.service.method;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.caldav.Filter.CompFilter;
/*     */ import com.zimbra.cs.dav.caldav.Range.ExpandRange;
/*     */ import com.zimbra.cs.dav.caldav.Range.TimeRange;
/*     */ import com.zimbra.cs.dav.resource.CalendarCollection;
/*     */ import com.zimbra.cs.dav.resource.CalendarObject;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalendarQuery
/*     */   extends Report
/*     */ {
/*     */   public void handle(DavContext ctxt)
/*     */     throws DavException, ServiceException
/*     */   {
/*  55 */     Element query = ctxt.getRequestMessage().getRootElement();
/*  56 */     if (!query.getQName().equals(DavElements.E_CALENDAR_QUERY)) {
/*  57 */       throw new DavException("msg " + query.getName() + " is not calendar-query", 400, null);
/*     */     }
/*     */     
/*     */ 
/*  61 */     DavContext.RequestProp reqProp = ctxt.getRequestProp();
/*  62 */     QueryContext qctxt = new QueryContext(ctxt, query, reqProp);
/*     */     
/*  64 */     if (qctxt.componentFilter == null) {
/*  65 */       throw new DavException("missing filter element in the request", 400, null);
/*     */     }
/*     */     
/*  68 */     DavResource rsc = ctxt.getRequestedResource();
/*  69 */     if (!(rsc instanceof CalendarCollection)) {
/*  70 */       throw new DavException("not a calendar resource", 400, null);
/*     */     }
/*     */     
/*  73 */     CalendarCollection cal = (CalendarCollection)rsc;
/*  74 */     Range.TimeRange tr = qctxt.componentFilter.getTimeRange();
/*  75 */     if (tr == null) {
/*  76 */       tr = new Range.TimeRange(rsc.getOwner());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  86 */     qctxt.davCtxt.setStatus(207);
/*  87 */     DavResponse resp = qctxt.davCtxt.getDavResponse();
/*  88 */     resp.getTop(DavElements.E_MULTISTATUS);
/*  89 */     for (DavResource calItem : cal.getChildren(ctxt, tr)) {
/*  90 */       handleCalendarItem(qctxt, calItem);
/*     */     }
/*     */   }
/*     */   
/*     */   private void handleCalendarItem(QueryContext ctxt, DavResource calItem) {
/*  95 */     if (!(calItem instanceof CalendarObject))
/*  96 */       return;
/*     */     try {
/*  98 */       CalendarObject calobj = (CalendarObject)calItem;
/*  99 */       if (!calobj.match(ctxt.componentFilter)) {
/* 100 */         return;
/*     */       }
/* 102 */       DavResponse resp = ctxt.davCtxt.getDavResponse();
/* 103 */       if (ctxt.expandRange != null) {
/* 104 */         calobj.expand(ctxt.expandRange);
/*     */       }
/* 106 */       resp.addResource(ctxt.davCtxt, calItem, ctxt.props, false);
/*     */     } catch (DavException de) {
/* 108 */       ZimbraLog.dav.error("can't get calendar item data", de);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class QueryContext {
/*     */     CalendarQuery.RequestedComponent requestedComponent;
/*     */     Range.ExpandRange expandRange;
/*     */     Filter.CompFilter componentFilter;
/*     */     DavContext davCtxt;
/*     */     DavContext.RequestProp props;
/*     */     
/*     */     QueryContext(DavContext ctxt, Element query, DavContext.RequestProp rp) throws DavException {
/* 120 */       this.davCtxt = ctxt;
/* 121 */       this.props = rp;
/*     */       
/* 123 */       for (Object o : query.elements()) {
/* 124 */         if ((o instanceof Element)) {
/* 125 */           elem = (Element)o;
/* 126 */           QName name = elem.getQName();
/* 127 */           if (name.equals(DavElements.E_FILTER)) {
/* 128 */             parseFilter(elem);
/* 129 */           } else if ((!name.equals(DavElements.E_TIMEZONE)) && 
/* 130 */             (name.equals(DavElements.E_PROP))) {
/* 131 */             for (Object obj : elem.elements()) {
/* 132 */               if ((obj instanceof Element)) {
/* 133 */                 elem = (Element)obj;
/* 134 */                 if (elem.getQName().equals(DavElements.E_CALENDAR_DATA)) {
/* 135 */                   parseCalendarData(elem);
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */       Element elem;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     private void parseCalendarData(Element cd)
/*     */     {
/* 154 */       Element comp = cd.element(DavElements.E_COMP);
/* 155 */       if (comp != null)
/* 156 */         this.requestedComponent = new CalendarQuery.RequestedComponent(comp);
/* 157 */       Element expand = cd.element(DavElements.E_EXPAND);
/* 158 */       if (expand != null)
/* 159 */         this.expandRange = new Range.ExpandRange(expand);
/*     */     }
/*     */     
/*     */     private void parseFilter(Element filter) {
/* 163 */       for (Object o : filter.elements()) {
/* 164 */         if ((o instanceof Element)) {
/* 165 */           Element e = (Element)o;
/* 166 */           if (e.getQName().equals(DavElements.E_COMP_FILTER)) {
/* 167 */             this.componentFilter = new Filter.CompFilter(e);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class RequestedComponent
/*     */   {
/*     */     String name;
/*     */     Set<String> props;
/*     */     Set<RequestedComponent> comps;
/*     */     
/*     */     RequestedComponent(Element elem)
/*     */     {
/* 183 */       this.props = new HashSet();
/* 184 */       this.comps = new HashSet();
/*     */     }
/*     */     
/*     */     void parse(Element elem)
/*     */     {
/* 189 */       this.name = elem.attributeValue("name");
/* 190 */       for (Object o : elem.elements()) {
/* 191 */         if ((o instanceof Element)) {
/* 192 */           Element e = (Element)o;
/* 193 */           QName qname = e.getQName();
/* 194 */           if (qname.equals(DavElements.E_PROP)) {
/* 195 */             this.props.add(e.attributeValue("name"));
/* 196 */           } else if (qname.equals(DavElements.E_COMP)) {
/* 197 */             this.comps.add(new RequestedComponent(e));
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/method/CalendarQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */